package com.example.gpssystem.controller;

import com.example.gpssystem.model.Courier;
import com.example.gpssystem.repository.mongo.CourierRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/couriers")
@CrossOrigin(origins = "http://localhost:5173")
public class CourierController {

    @Autowired
    private CourierRepository courierRepository;

    @PostMapping
    public Courier createCourier(@RequestBody Courier courier) {
        return courierRepository.save(courier);
    }

    @GetMapping("/{id}")
    public Courier getCourier(@PathVariable String id) {
        return courierRepository.findById(id)
            .orElseThrow(() -> new RuntimeException("Courier not found"));
    }

    @PutMapping("/{id}")
    public Courier updateCourier(@PathVariable String id, @RequestBody Courier courier) {
        courier.setId(id);
        return courierRepository.save(courier);
    }
}